package com.mind.quest;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mind.common.GameConfig;
import com.mind.login.model.Player;
import com.mind.quest.contants.IQuestConstants.QuestStatus;
import com.mind.quest.model.QuestModel;

/**
 * 
 * @author shaobo.yang
 *
 */
public class QuestManager {
	private static final Logger logger = LoggerFactory.getLogger(QuestManager.class);
	public static void dispatchEvent(IEvent event,Player player){
		List<Integer> conditonQuestIds=GameConfig.questConfig.getEvent2QuestCondition().get(event.getId());
		boolean isChange=false;
		for(Integer questId:conditonQuestIds){
			IQuest quest=GameConfig.questConfig.getQuests().get(questId);
			acceptQuest(player, quest);
			isChange=true;
		}
		List<Integer> targetQuestIds=GameConfig.questConfig.getEvent2QuestTarget().get(event.getId());
		for(Integer questId:conditonQuestIds){
			IQuest quest=GameConfig.questConfig.getQuests().get(questId);
			execQuest(player, quest,event);
			isChange=true;
		}
		if(isChange){
			player.update();
		}
	}
	private static void execQuest(Player player,IQuest quest,IEvent event){
		QuestModel model=player.quest.getQuests().get(quest.getId());
		if(model==null){//该任务没有正在进行中
			return ;
		}
		List<ITarget> targets=quest.getTargets();
		boolean isFinished=true;
		for(ITarget target:targets){
			if(checkTargetIsFinished(model, target)){
				continue;
			}
			List<Integer> targetIds=GameConfig.questConfig.getEvent2Targets().get(event.getId());
			if(targetIds.contains(target.getId())){
				if(checkIsTriggerTarget(event, target)){
					switch(target.getTargetValueType()){
					case ADD_UP:
						model.getValue().put(quest.getId(), model.getValue().get(target.getId())+event.getValue());
						break;
					case REPLACE:
						model.getValue().put(quest.getId(),event.getValue());
						break;
					}
				}
			}
			if(!checkTargetIsFinished(model, target)){
				isFinished=false;
			}
		}
		if(isFinished){
			model.setStatus(QuestStatus.FINISHED);
		}
	}
	/**检查该事件吃否会触发该目标
	 * @param event
	 * @param target
	 * @return
	 */
	private static boolean checkIsTriggerTarget(IEvent event,ITarget target){
		Map<Integer, ITargetCondition> targetConditions=target.getConditions();
		Map<Integer, IEventCondition> eventConditions=event.getConditions();
		for(Integer conditionId:targetConditions.keySet()){
			if(!eventConditions.containsKey(conditionId)){
				logger.error("该事件传递的条件不完整。eventId="+event.getId()+",targetId="+target.getId());
				return false;
			}
			ITargetCondition targetConditon=targetConditions.get(conditionId);
			IEventCondition eventConditon=eventConditions.get(conditionId);
			switch(targetConditon.getConditionType()){
			case STRING_EQUAL:
				String targetValue=	(String) targetConditon.getConditionValue();
				String eventValue=	(String) eventConditon.getConditionValue();
				if(!targetValue.equals(eventValue))
					return false;
				break;
			case GREATER_THAN:
				int targetValue1=	(Integer) targetConditon.getConditionValue();
				int eventValue1=	(Integer) eventConditon.getConditionValue();
				if(eventValue1<=targetValue1)
					return false;
				break;
			case INT_EQUAL:
				targetValue1=	(Integer) targetConditon.getConditionValue();
				eventValue1=	(Integer) eventConditon.getConditionValue();
				if(eventValue1!=targetValue1)
					return false;
				break;
			case LESS_THAN:
				targetValue1=	(Integer) targetConditon.getConditionValue();
				eventValue1=	(Integer) eventConditon.getConditionValue();
				if(eventValue1>=targetValue1)
					return false;
				break;
			case SPECIAL:
				logger.error("事件以及目标的条件中存在特殊类型条件！targetId="+target.getId()+",conditonId="+targetConditon.getId());
				break;
			}
		}
		return true;
	}
	
	
	
	
	/**判断该目标是否完成
	 * @param target
	 * @param questModel
	 * @return
	 */
	private static boolean checkTargetIsFinished(QuestModel questModel,ITarget target){
		if(questModel.getValue().get(target)>= target.getTargetValue()){
			return true;
		}
		return false;
	}
	
	private static boolean checkCanAcceptQuest(Player player,IQuest quest){
		if(player.quest.getQuests().containsKey(quest.getId())){//该任务正在进行中
			return false;
		}
		List<IAcceptQuestConditon> conditions=	quest.getConditions();
		for(IAcceptQuestConditon condition:conditions){
			if(!condition.check(player)){
				return false;
			}
		}
		return true;	
	}
	private static void acceptQuest(Player player,IQuest quest){
		if(checkCanAcceptQuest(player, quest)){
			QuestModel questModel=new QuestModel(quest.getId());
			List<ITarget> targets=	quest.getTargets();
			for(ITarget target:targets){
				questModel.getValue().put(target.getId(), 0);
			}
			questModel.setStatus(QuestStatus.PROGRESS);
			player.quest.getQuests().put(quest.getId(), questModel);
		}
	}
	
	
}
